package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.algorithm.performance.CPUEvaluator;
import cn.edu.nju.software.cdiy.bean.BRD_CPU;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.CPU;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.MB_CPUXL;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.CPUDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class CPUDaoImpl implements CPUDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<CPU> getCPUList(int page, SortType s, HardwareSet hs, SortWay sw) {
		// System.out.println("进入CPUdaoimpl getCPUlist");

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<CPU> list = null;

		// 第page页的第minNum条记录到maxNum条记录
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		switch (s) {
		case PRICE:
			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY OPRICE DESC";
			list = sess.createQuery(sql).list();
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			list = sess.createQuery(sql).list();
			break;
		case W_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY WSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case Z_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY ZSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case NEW:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			list = sess.createQuery(sql).list();

			break;
		case HOT:
			sql = "FROM CPU WHERE STATE=882";
			list = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(list);
			else {
				Collections.sort(list, new SortByHotAsc());
			}
			break;
		default:
			break;

		}
		list = filterList(hs, list, minNum, maxNum);

		return list;
	}

	private List<CPU> filterList(HardwareSet hs, List<CPU> list, int min,
			int max) {

		/*
		 * 分别由各部件的filter对待检测的部件表进行筛选 目前只有一个条件可以用来筛选 MainBoard
		 */
		list = uniFilter(hs, list);
		// 最终根据页数的要求返回list
		List<CPU> returnList = new Pager<CPU>().getPageList(list, min, max);
		return returnList;
	}

	private List<CPU> mainBoardFilter(HardwareSet hs, List<CPU> l) {
		/*
		 * 主板对CPU的限制 cpu系列
		 */
		if (hs.getMainBoard() != null) {
			MainBoard mb = hs.getMainBoard();
			int cpumanu = mb.getCPUManu().getId();
			// 获取主板支持的CPU系列
			Session sess = sessionFactory.getCurrentSession();
			int id = mb.getId();
			String sql = "FROM MB_CPUXL WHERE MB=" + id;
			List<MB_CPUXL> list = sess.createQuery(sql).list();

			List<CPU> returnList = new ArrayList<CPU>();

			for (int i = 0; i < l.size(); i++) {
				// 遍历原表进行筛选
				CPU nowCPU = l.get(i);
				int cpuxl = nowCPU.getCPUXL().getKey();
				// 先判断CPU的品牌是否符合主板支持的CPU生产厂商
				if (l.get(i).getBrand().getId() == cpumanu) {
					for (int j = 0; j < list.size(); j++) {
						// 检测此CPU是否是这个主板支持的cpu系列
						if (list.get(j).getCpuxl().getKey() == cpuxl) {
							// 若支持这个系列 则加入返回list 并跳出进行下一个CPU的检验
							returnList.add(nowCPU);
							break;
						} else
							continue;
					}
				}
			}

			return returnList;
		} else
			return l;
	}

	@Override
	public List<Hardware> getNewestList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM CPU WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
		List<CPU> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);

		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM CPU WHERE STATE=882";
		List<CPU> list = sess.createQuery(sql).list();

		Collections.sort(list);
		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);

		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_CPU";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_CPU> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		return returnList;
	}

	@Override
	public List<CPU> filter(int page, Map<String, List<Integer>> map,
			SortType st, SortWay sw, HardwareSet hs) {

		// 搜索时的过滤方法
		if (map == null || map.size() == 0)
			return getCPUList(page, st, hs, sw);

		// 获得此产品表
		List<CPU> hwList = getFilterList(hs, st, sw);

		// 最后的匹配表
		List<CPU> matchList = new ArrayList<CPU>();

		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.cpuItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.cpuItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.CPU_PRICE[list.get(k) - 1].getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCPUXL().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCoreNum().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getManuTech().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[5])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getWordSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[6])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCoreType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<CPU>();
			}
		}
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);
		matchList = new Pager<CPU>().getPageList(matchList, minNum, maxNum);
		return matchList;
	}

	private Value getValueByID(Integer integer) {

		Session sess = sessionFactory.getCurrentSession();
		Value c = (Value) sess.load(Value.class, integer);

		return c;
	}

	private BRD_CPU getBrandById(Integer integer) {

		Session sess = sessionFactory.getCurrentSession();
		BRD_CPU c = (BRD_CPU) sess.load(BRD_CPU.class, integer);

		return c;
	}

	private List<CPU> getFilterList(HardwareSet hs, SortType st, SortWay sw) {

		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */
		List<CPU> l = new ArrayList<CPU>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:

			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY OPRICE DESC";
			l = sess.createQuery(sql).list();

			break;
		case W_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY WSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case Z_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY ZSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case NEW:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			l = sess.createQuery(sql).list();

			break;
		case HOT:

			sql = "FROM CPU WHERE STATE=882";
			l = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(l);
			else {
				Collections.sort(l, new SortByHotAsc());
			}

			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM CPU WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM CPU WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			l = sess.createQuery(sql).list();
			break;
		default:
			break;

		}

		l = uniFilter(hs, l);
		return l;
	}

	@Override
	public List<CPU> search(String keywords, int page) {

		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM CPU WHERE STATE=882";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<CPU> list = sess.createQuery(sql).list();

		List<CPU> returnList = new Pager<CPU>().getPageList(list, minNum,
				maxNum);

		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {

		HashMap<String, List<Item>> map = new HashMap<String, List<Item>>();
		map.put(getBrandItem().getItemName(), getFilterBrandItem());
		map.put(getPriceItem().getItemName(), getFilterPriceItem());

		for (int i = 2; i < Constant.cpuItemNameList.length; i++) {
			map.put(Constant.cpuItemNameList[i], getFilterItemByNum(i));
		}

		return map;
	}

	private List<Item> getFilterItemByNum(int i) {

		Session sess = sessionFactory.getCurrentSession();

		String sql = null;
		List<Value> values = null;
		List<Item> choices = null;

		switch (i) {
		case 2:
			// cpu系列

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=106 AND KEY<=160 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 3:
			// 核心数量

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=921 AND KEY<=930 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 4:
			// 制作工艺

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=171 AND KEY<=180 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 5:
			// 字长

			// itemName

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=181 AND KEY<=190 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 6:
			// 核心类型

			// itemName

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=161 AND KEY<=170 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		default:
			break;

		}

		return choices;
	}

	private List<Item> getFilterPriceItem() {

		List<Item> choices = new ArrayList<Item>();
		for (int i = 0; i < (Constant.CPU_PRICE.length); i++) {
			choices.add(Constant.CPU_PRICE[i]);
		}
		return choices;
	}

	private List<Item> getFilterBrandItem() {

		Session sess = sessionFactory.getCurrentSession();

		String sql = "FROM BRD_CPU";
		List<BRD_CPU> brandList = sess.createQuery(sql).list();
		List<Item> choices = new ArrayList<Item>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(new Item(brandList.get(i).getBrand(), brandList.get(i)
					.getId()));
		}

		return choices;
	}

	private FilterItem getItemByNum(int i) {

		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.cpuItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<String> choices = null;

		switch (i) {
		case 2:
			// cpu系列

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=106 AND KEY<=160 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 3:
			// 核心数量

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=921 AND KEY<=930 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 4:
			// 制作工艺

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=171 AND KEY<=180 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 5:
			// 字长

			// itemName

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=181 AND KEY<=190 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 6:
			// 核心类型

			// itemName

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=161 AND KEY<=170 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		default:
			break;

		}
		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getPriceItem() {

		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.cpuItemNameList[1]);

		List<String> choices = new ArrayList<String>();
		for (int i = 0; i < (Constant.CPU_PRICE.length); i++) {
			choices.add(Constant.CPU_PRICE[i].getName());
		}

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {

		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.cpuItemNameList[0]);

		String sql = "FROM BRD_CPU";
		List<BRD_CPU> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);

		return fi;
	}

	@Override
	public CPU getOneById(int id) {

		Session sess = sessionFactory.getCurrentSession();
		CPU c = (CPU) sess.load(CPU.class, id);

		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {

		// 返回的map
		Map<String, List<String>> rMap = new HashMap<String, List<String>>();

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<String> l = null;
		List<Value> valueList = null;

		// 品牌
		sql = "FROM BRD_CPU";
		l = new ArrayList<String>();
		List<BRD_CPU> brandList = sess.createQuery(sql).list();
		for (int i = 0; i < brandList.size(); i++) {
			l.add(brandList.get(i).getBrand());
		}
		rMap.put(Constant.cpuSelectStr[0], l);

		// CPU系列
		rMap.put(Constant.cpuSelectStr[1], getValueByKey(106, 160));

		// 核心数量
		rMap.put(Constant.cpuSelectStr[5], getValueByKey(921, 930));

		// 制作工艺
		rMap.put(Constant.cpuSelectStr[3], getValueByKey(171, 180));

		// 字长
		rMap.put(Constant.cpuSelectStr[4], getValueByKey(181, 190));

		// 核心类型
		rMap.put(Constant.cpuSelectStr[2], getValueByKey(161, 170));

		// 线程数
		l = getValueListByKey(191, 200);
		rMap.put(Constant.cpuSelectStr[6], l);

		// 1级缓存
		l = getValueListByKey(201, 205);
		rMap.put(Constant.cpuSelectStr[7], l);
		// 2级缓存
		l = getValueListByKey(206, 215);
		rMap.put(Constant.cpuSelectStr[8], l);
		// 3级缓存
		l = getValueListByKey(216, 225);
		rMap.put(Constant.cpuSelectStr[9], l);
		// 显示核心
		l = getValueListByKey(226, 230);
		rMap.put(Constant.cpuSelectStr[10], l);
		// 插槽类型
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_CPU=1 ORDER BY INTF_TYPE,NAME";
		List<Interface> list = sess.createQuery(sql).list();
		for (int i = 0; i < list.size(); i++) {
			l.add(list.get(i).getName());
		}
		rMap.put(Constant.cpuSelectStr[11], l);

		return rMap;
	}

	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		try {
			// TODO Auto-generated method stub
			Session sess = sessionFactory.getCurrentSession();
			CPU cpu = new CPU();
			// 本方法处理外键和专属属性，通用属性由其他方法提供
			// CPU没有一对多的情况
			Iterator i = map.keySet().iterator();
			while (i.hasNext()) {
				String key = i.next().toString();
				List<String> list = map.get(key);
				String sql = null;
				if (key.equals(Constant.cpuInforStr[0])) {
					cpu.setModel(list.get(0));
					// 外键
				} else if (key.equals(Constant.cpuSelectStr[0])) {
					sql = "FROM BRD_CPU WHERE BRAND='" + list.get(0) + "'";
					List<BRD_CPU> bl = sess.createQuery(sql).list();
					cpu.setBrand(bl.get(0));
				} else if (key.equals(Constant.cpuSelectStr[1])) {
					cpu.setCPUXL(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[2])) {
					cpu.setCoreType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[3])) {
					cpu.setManuTech(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[4])) {
					cpu.setWordSize(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[5])) {
					cpu.setCoreNum(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[6])) {
					cpu.setThreadNum(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[7])) {
					cpu.setL1(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[8])) {
					cpu.setL2(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[9])) {
					cpu.setL3(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[10])) {
					cpu.setDisplayCoreType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.cpuSelectStr[11])) {
					cpu.setSlotType(getInterfaceByName(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[1])) {
					// 非外键
					cpu.setMasterFrequency(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[2])) {
					cpu.setMaxMFrequency(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[3])) {
					cpu.setFrequencyMulti(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[4])) {
					cpu.setCoreVolt(list.get(0));
				} else if (key.equals(Constant.cpuInforStr[5])) {
					cpu.setTURBOBOOST(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[6])) {
					cpu.setHT(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[7])) {
					cpu.setDisplayCoreFrequency(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[8])) {
					cpu.setTDP(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.cpuInforStr[9])) {
					cpu.setVIRTUALIZATION(Integer.parseInt(list.get(0)));
				} else {
					// 通用属性
					Changer.setUniAttribute(cpu, key, list);
				}

			}
			Changer.setDefaultAttribute(cpu);
			cpu.setState(getValueByID(Constant.HW_DEFAULT_STATE));
			// 最后填充性能
			cpu.setPerformance(new CPUEvaluator().getPerformance(cpu));

			sess.save(cpu);
			sess.flush();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private List<String> getValueListByKey(int min, int max) {
		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + min + " AND KEY<=" + max
				+ " ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int i = 0; i < l.size(); i++) {
			rl.add(l.get(i).getValue());
		}

		return rl;
	}

	private Value getValueByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='" + name + "'";
		List<Value> l = sess.createQuery(sql).list();

		return l.get(0);

	}

	private List<String> getValueByKey(int i, int j) {

		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + i + " AND KEY<=" + j
				+ " ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int k = 0; k < l.size(); k++) {
			rl.add(l.get(k).getValue());
		}

		return rl;
	}

	private Interface getInterfaceByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='" + name + "'";
		List<Interface> l = sess.createQuery(sql).list();

		return l.get(0);

	}

	@Override
	public void save(CPU t) {

		Session sess = sessionFactory.getCurrentSession();

		sess.saveOrUpdate(t);
		sess.flush();

	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {

		List<CPU> hwList = null;
		List<CPU> matchList = new ArrayList<CPU>();

		String sql = "FROM CPU WHERE STATE=882";
		Session sess = sessionFactory.getCurrentSession();
		hwList = sess.createQuery(sql).list();

		hwList = uniFilter(hs, hwList);

		if (map == null || map.size() == 0)
			return hwList.size();
		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.cpuItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.cpuItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.CPU_PRICE[list.get(k) - 1].getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCPUXL().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCoreNum().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getManuTech().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[5])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getWordSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.cpuItemNameList[6])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getCoreType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<CPU>();
			}
		}

		return matchList.size();

	}

	@Override
	public int getListPageNum(HardwareSet hs) {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM CPU WHERE STATE=882";
		List<CPU> list = sess.createQuery(sql).list();
		list = uniFilter(hs, list);

		return list.size();
	}

	private List<CPU> uniFilter(HardwareSet hs, List<CPU> list) {
		if (hs != null) {
			list = mainBoardFilter(hs, list);
		}
		return list;
	}

	@Override
	public List<CPU> getOneBySupplierID(int id) {
		// TODO Auto-generated method stub
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM CPU where SUPPLIER=" + id;
		List<CPU> list = sess.createQuery(sql).list();

		return list;
	}
}
